package ServerMain;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

@WebServlet("/Service")
public class Server extends HttpServlet
{
	//Database
	DataBaseConnection database;

	public Server()
	{
		database = new DataBaseConnection();
	}
	
	//Handling of all incoming requests (many different threads enter here)
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("application/vnd.ms-excel");
      
		PrintWriter out = response.getWriter();

		String requestType = request.getHeader(GTK.NetworkProtocol.REQUEST_HEADER);
		System.out.println("Request: " + requestType);
		
		//Create account
		if(requestType.equals(GTK.NetworkProtocol.CREATE_ACCOUNT_REQUEST))
		{
			String userName = request.getHeader(GTK.NetworkProtocol.NAME_HEADER);
			String password = request.getHeader(GTK.NetworkProtocol.PASSWORD_HEADER);
			
			//Create the new user
			if(!database.containsUser(userName))
			{
				database.addUser(userName, password);
				
				out.println(GTK.NetworkProtocol.SUCCESS);
				System.out.println("Successfully added: " + userName + " to the database");
			}
			//User already existed, dont create the new user
			else
			{
				out.println(GTK.NetworkProtocol.USER_ALREADY_EXISTS);
				System.out.println(userName + " already existed in the database");
			}	
		}
		else if(requestType.equals(GTK.NetworkProtocol.EARN_ACHIEVEMENT_REQUEST))
		{
			String userName = request.getHeader(GTK.NetworkProtocol.NAME_HEADER);
			String password = request.getHeader(GTK.NetworkProtocol.PASSWORD_HEADER);
			byte achievementID = Byte.parseByte(request.getHeader(GTK.NetworkProtocol.ACHIEVEMENT_ID_HEADER));
			
			if(database.authenticate(userName, password))
			{
				if(database.awardAchievement(userName, achievementID))
				{
					out.println(GTK.NetworkProtocol.SUCCESS);
					System.out.println("Successfully awarded: TestA" + achievementID + " to " + userName);
				}
				else
				{
					out.println(GTK.NetworkProtocol.UNKNOWN_ERROR);
					System.out.println("Failed to awarded: TestA" + achievementID + " to " + userName);
				}
			}
		}
		else if(requestType.equals(GTK.NetworkProtocol.GET_ACHIEVEMENTS_REQUEST))
		{
			String userName = request.getHeader(GTK.NetworkProtocol.NAME_HEADER);
			String password = request.getHeader(GTK.NetworkProtocol.PASSWORD_HEADER);
			
			if(database.authenticate(userName, password))
			{
				out.println(GTK.NetworkProtocol.SUCCESS);
				String achievementsToSend = database.getAchievements(userName);
				System.out.println("Achievements sent to userName: " + achievementsToSend);
				out.println(achievementsToSend);
			}
			else
			{
				out.println(GTK.NetworkProtocol.WRONG_ACCOUNT_OR_PASSWORD);
			}
		}
	}
 
	
	private void earnAchievement()
	{
		//database.awardAchievement(userName, achievementID)
	}
	
	/**
	 * Checks with the database if a connection has provided correct login information. This method is thread safe.
	 * @param out - A PrintWriter connected to the user used for sending the result of the request.
	 * @param name - The players account name.
	 * @param password - The players password.
	 * @return Returns whether the user exists and have the provided password
	 */
    private boolean authenticate(PrintWriter out, String userName, String password)
	{
    	boolean success = database.authenticate(userName, password); 
		System.out.println(
			(success ? "Authenticated: " : "Failed to Authenticate") + 
			"---Name: " + userName + ", Pass: " + password + "---");
		return success;
	}
	
	public static void main(String[] args)
	{
		new Server();
	}
}
